package com.tensquare.friend.dao;

import com.tensquare.friend.pojo.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * @Author:zimmor
 * @Date: 2021/3/15 19:18
 * @Description 数据层
 * @version: 1.0.0
 */

public interface FriendDao extends JpaRepository<Friend, String>, JpaSpecificationExecutor<Friend> {
    /**
     * 判断被点赞的用户是否是当前用户的好友
     * @return 返回1是 返回0不是
     */
    @Query(value = "SELECT COUNT(*) FROM tb_friend WHERE userid = ? AND friendid = ?", nativeQuery = true)
    public int findFriendCount(String userid, String friendid);

    /**
     * 更新为单项喜欢还是相互喜欢
     * @param userid
     * @param friendid
     * @param type
     */
    @Query(value = "UPDATE tb_friend SET islike = ?3  WHERE userid = ?1 AND friendid = ?2", nativeQuery = true)
    @Modifying
    public void updateFriend(String userid, String friendid, String type);

    /**
     * 删除好友
     * @param userid
     * @param friendid
     */
    @Query(value="DELETE FROM tb_friend WHERE userid= ? AND friendid = ?", nativeQuery=true)
    @Modifying
    void deleteFriend(String userid, String friendid);
}
